package com.wxprogram.smallprogram.service.impl;

import com.wxprogram.smallprogram.dao.IFindstudent;
import com.wxprogram.smallprogram.dao.IFindteacher;
import com.wxprogram.smallprogram.dao.MajorDao;
import com.wxprogram.smallprogram.entity.FindStudent;
import com.wxprogram.smallprogram.entity.FindTeacher;
import com.wxprogram.smallprogram.entity.utilEntity.PageUtil;
import com.wxprogram.smallprogram.service.FileService;
import com.wxprogram.smallprogram.service.ITeacherDeliverReqService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TeacherDeliverReqServiceImpl implements ITeacherDeliverReqService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
    private IFindstudent iFindstudent;
    @Autowired
    private IFindteacher iFindteacher;
    @Autowired
	private MajorDao majorDao;

	@Resource
	private RedisTemplate<String,Object> redisTemplate;

    /**
     * 插入学长发表信息
     * @param findstudent
     * @return
     */
    public int insertFindstudent(FindStudent findstudent){
    	//根据专业查询类型并赋值
    	String major = findstudent.getTeachermajor();
    	findstudent.setTeachermajortype(majorDao.selectMajorTypeByMajor(major));
		int teachercode = findstudent.getTeachercode();
		String key="findstudent_"+teachercode;
		ValueOperations<String, Object> operations = redisTemplate.opsForValue();
		boolean hasKey = redisTemplate.hasKey(key);
		if(hasKey){
			return  iFindstudent.insertFindstudent(findstudent);
		}else{
			operations.set(key,findstudent);
			logger.info("插入缓存key:"+key);
			return  iFindstudent.insertFindstudent(findstudent);
		}

    }

    /**
     * 根据openID查询所有发布
     */
	@Override
	public List<FindStudent> getTeacherPublish(String openID) {
		List<FindStudent> list1 = new ArrayList<>();
		List<Integer> list2 = new ArrayList<>();
		list2 = iFindstudent.selectTeaCodeByOpenId(openID);
		ValueOperations<String, Object> operations = redisTemplate.opsForValue();
		for (Integer TeaCode : list2) {
			FindStudent findStudent = (FindStudent) operations.get("findstudent_" + TeaCode);
			if(findStudent!=null) {
				logger.info("从缓存中获取数据: findstudent_" + TeaCode);
				list1.add(findStudent);
			}
		}
		if (list1.size() == list2.size()) {
			logger.info("从缓存获取数据成功");
			return list1;
		} else {
			logger.info("缓存无数据，从数据库中获取");
			return iFindstudent.selectFindstudentByOpenid(openID);
		}
	}
	/**
	 * 分页查询
	 */
	@Override
	public PageUtil<FindTeacher> getDeliver(String city, String school, String majortype, String major,int current) {
		PageUtil<FindTeacher> page = new PageUtil<>(current);
		FindTeacher findTeacher = new FindTeacher();
		findTeacher.setStucity(city);
		findTeacher.setStuschool(school);
		findTeacher.setStumajortype(majortype);
		findTeacher.setStumajor(major);
		page.setCount(iFindteacher.getCount(findTeacher));
		page.setList(iFindteacher.selectFindteacherList(findTeacher,page.getOffset(),page.getSize()));
		return page;
	}

	/**
	 * 获取详细信息
	 * @param code
	 * @return
	 */
	@Override
	public FindTeacher getDetail(int code){
		return iFindteacher.selectFindteacherByTeachercode(code);
	}

	@Override
	public int deletePub(int code) {
		String key = "findstudent_"+ code;
		ValueOperations<String, Object> operations = redisTemplate.opsForValue();
		boolean hasKey = redisTemplate.hasKey(key);
		if(hasKey){
			redisTemplate.delete(key);
			logger.info("更新用户时，从缓存中删除用户 >> " +key);
		}

		int count = iFindstudent.deleteFindstudentByCode(code);

//		if (count > 0){
//			final int _code = code;
//			new Thread(new Runnable(){
//				@Autowired
//				private FileService fileService;
//
//				@Override
//				public void run() {
//					String path = iFindstudent.selectImgByCode(_code);
//					fileService.deleteFile(path);
//				}
//			}).start();
//		}

		return count;
	}

	/**
	 * 修改查看数
	 *
	 * @param code
	 * @return
	 */
	@Override
	public int updateViewNum(int code) {
		return iFindstudent.updateViewNum(code);
	}
}
